import {
  Controller,
  Post,
  Get,
  Body,
  Query,
  Patch,
  Param,
  ParseIntPipe,
  Delete,
} from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { FindAllDto, UpdateUserDto } from './dto/update-user.dto';
import { Permissions } from '../public/public.decorator';
import { LogOperationTitle } from 'src/common/decorator/oprertionlog.decorator';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  @LogOperationTitle('创建用户')
  @Permissions('create')
  @ApiOperation({ summary: '创建用户' })
  create(@Body() createUserDto: CreateUserDto) {
    console.log(createUserDto);
    return this.userService.create(createUserDto);
  }

  @Permissions('read')
  @Get('get-all-users')
  @LogOperationTitle('获取所有用户')
  @ApiOperation({ summary: '获取所有用户' })
  createAdmin(@Query() createUserDto: FindAllDto) {
    return this.userService.findAll(createUserDto);
  }

  // 更新用户信息
  @Patch(':id')
  @Permissions('update')
  @ApiOperation({ summary: '更新用户信息' })
  @LogOperationTitle('更新用户信息')
  updateUser(
    @Param('id', ParseIntPipe) id: number,
    @Body() createUserDto: UpdateUserDto,
  ) {
    console.log(createUserDto);
    return this.userService.update(id, createUserDto);
  }

  // 删除用户
  @Delete(':id')
  @Permissions('delete')
  @ApiOperation({ summary: '删除用户' })
  @LogOperationTitle('删除用户')
  deleteUser(@Param('id', ParseIntPipe) id: number) {
    return this.userService.remove(id);
  }
}
